package com.zsyh.changshang.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zsyh.changshang.R;
import com.zsyh.changshang.adepter.ErjiAdepter;
import com.zsyh.changshang.basic.BaseFragment;
import com.zsyh.changshang.basic.PhoneContract;
import com.zsyh.changshang.basic.PhonePresenter;
import com.zsyh.changshang.basic.RetrofitHelper;
import com.zsyh.changshang.basic.Viewable;
import com.zsyh.changshang.bean.WeizhifuBean;
import com.zsyh.changshang.bean.ZhongleiBean;
import com.zsyh.changshang.data.DataApi;
import com.zsyh.changshang.util.SPUtils;
import com.zsyh.changshang.weight.DialogManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

@Viewable(presenter = PhonePresenter.class, layout = R.layout.orderlayout)
public class orderFragment extends BaseFragment<PhoneContract.View, PhoneContract.Presenter> implements PhoneContract.View {


    @BindView(R.id.retob)
    RelativeLayout retob;
    @BindView(R.id.tv_wfh)
    TextView tvWfh;
    @BindView(R.id.xian_wfh)
    View xianWfh;
    @BindView(R.id.re_wfh)
    RelativeLayout reWfh;
    @BindView(R.id.tv_wfk)
    TextView tvWfk;
    @BindView(R.id.xian_wfk)
    View xianWfk;
    @BindView(R.id.re_wfk)
    RelativeLayout reWfk;
    @BindView(R.id.tv_lishi)
    TextView tvLishi;
    @BindView(R.id.xian_lishi)
    View xianLishi;
    @BindView(R.id.re_lsfh)
    RelativeLayout reLsfh;
    @BindView(R.id.lin_shuzhi)
    LinearLayout linShuzhi;
    @BindView(R.id.zhongleirecy)
    RecyclerView zhongleirecy;
    @BindView(R.id.liebiaorecey)
    RecyclerView liebiaorecey;
    Unbinder unbinder;
    private DataApi dataApi;
    private DialogManager mDialogManager;
    private CommonAdapter<ZhongleiBean.DataBean> zhongleiAdepter;
    private List<ZhongleiBean.DataBean> zhongleiList=new ArrayList<>();
    private List<Integer> list1 = new ArrayList<>();
    private ErjiAdepter erjiAdepter;
    private int type=1;
    @Override
    public void init() {
        dataApi = RetrofitHelper.getInstance().getRetrofit().create(DataApi.class);
        mDialogManager = new DialogManager();
        getpeijianMoudle((String) SPUtils.get(getActivity(), "token", ""));
        tvWfh.setTextColor(getResources().getColor(R.color.lanse));
        xianWfh.setBackgroundColor(getResources().getColor(R.color.lanse));
        tvWfk.setTextColor(getResources().getColor(R.color.huise));
        xianWfk.setBackgroundColor(getResources().getColor(R.color.white));
        tvLishi.setTextColor(getResources().getColor(R.color.huise));
        xianLishi.setBackgroundColor(getResources().getColor(R.color.white));
    }



    /**
     * 获取配件种类
     */
    public void getpeijianMoudle(String token) {
        mDialogManager.showLoadingDialogFragment(getActivity().getSupportFragmentManager());
        dataApi = RetrofitHelper.getInstance().getRetrofit().create(DataApi.class);
        dataApi.getAllFactoryParts(token)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        mDialogManager.hideLoadingDialogFragment();
                        try {
                            String body = responseBody.string();
                            Log.e("zhonglei", body + "");
                            JSONObject jsonObject = new JSONObject(body);
                            if (jsonObject.getString("errorCode").equals("0")) {
                                Gson gson = new Gson();
                                ZhongleiBean bean = gson.fromJson(body, ZhongleiBean.class);
                                if(zhongleiList!=null){
                                    zhongleiList.clear();
                                }
                                if (list1 != null) {
                                    list1.clear();
                                }
                                for(int i=0;i<bean.getData().size();i++){
                                    zhongleiList.add(bean.getData().get(i));
                                    if(i==0){
                                        list1.add(1);
                                    }else{
                                        list1.add(0);
                                    }

                                }
                                getweifahuo((String) SPUtils.get(getActivity(), "token", ""),zhongleiList.get(0).getId());
                                erjiAdepter=new ErjiAdepter(getActivity(),R.layout.item_zhonglei,zhongleiList,list1);
                                erjiAdepter.setOnItemClickListener(new com.zsyh.changshang.adepter.CommonAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(View view, int position) {
                                        setpostion12(list1,position);
                                        erjiAdepter.notifyDataSetChanged();
                                        getweifahuo((String) SPUtils.get(getActivity(), "token", ""),zhongleiList.get(position).getId());
                                    }
                                });

                                LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
                                layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                                zhongleirecy.setLayoutManager(layoutManager);
                                zhongleirecy.setAdapter(erjiAdepter);
                                //zhongleiAdepter.notifyDataSetChanged();
                            } else {
                                //ToastUtil.showS(MyAppliaction.getContext(),jsonObject.getString("msg"));
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mDialogManager.hideLoadingDialogFragment();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @OnClick({R.id.re_wfh, R.id.re_wfk, R.id.re_lsfh})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.re_wfh:
                type=1;
                tvWfh.setTextColor(getResources().getColor(R.color.lanse));
                xianWfh.setBackgroundColor(getResources().getColor(R.color.lanse));
                tvWfk.setTextColor(getResources().getColor(R.color.huise));
                xianWfk.setBackgroundColor(getResources().getColor(R.color.white));
                tvLishi.setTextColor(getResources().getColor(R.color.huise));
                xianLishi.setBackgroundColor(getResources().getColor(R.color.white));
                break;
            case R.id.re_wfk:
                type=2;
                tvWfh.setTextColor(getResources().getColor(R.color.huise));
                xianWfh.setBackgroundColor(getResources().getColor(R.color.white));
                tvWfk.setTextColor(getResources().getColor(R.color.lanse));
                xianWfk.setBackgroundColor(getResources().getColor(R.color.lanse));
                tvLishi.setTextColor(getResources().getColor(R.color.huise));
                xianLishi.setBackgroundColor(getResources().getColor(R.color.white));

                break;
            case R.id.re_lsfh:
                type=3;
                tvWfh.setTextColor(getResources().getColor(R.color.huise));
                xianWfh.setBackgroundColor(getResources().getColor(R.color.white));
                tvWfk.setTextColor(getResources().getColor(R.color.huise));
                xianWfk.setBackgroundColor(getResources().getColor(R.color.white));
                tvLishi.setTextColor(getResources().getColor(R.color.lanse));
                xianLishi.setBackgroundColor(getResources().getColor(R.color.lanse));
                break;
        }
    }

    public void setpostion12(List<Integer> list, int postion) {
        for (int i = 0; i < list.size(); i++) {
            if (i == postion) {
                list.set(postion, 1);
            } else {
                list.set(i, 0);
            }
        }

    }


    /**
     * 获取未发货订单
     */
    public  void getweifahuo(String token,String id){
        mDialogManager.showLoadingDialogFragment(getActivity().getSupportFragmentManager());
        dataApi = RetrofitHelper.getInstance().getRetrofit().create(DataApi.class);
        dataApi.getUnSendPartsOrders(token,id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        mDialogManager.hideLoadingDialogFragment();
                        try {
                            String body = responseBody.string();
                            Log.e("weifahuo", body + "");
                            JSONObject jsonObject = new JSONObject(body);
                            if (jsonObject.getString("errorCode").equals("0")) {

                                Gson gson=new Gson();
                                WeizhifuBean weizhifuBean=gson.fromJson(body,WeizhifuBean.class);

                            } else {
                                //ToastUtil.showS(MyAppliaction.getContext(),jsonObject.getString("msg"));
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }




}
